/*
 * Duplicate Detector Copyright (C) 2010 Marco Biscaro <marcobiscaro2112@gmail.com>
 * 
 * This file is part of Duplicate Detector.
 *
 * Duplicate Detector is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Duplicate Detector is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Duplicate Detector.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.googlecode.duplicatedetector.view;

import static com.googlecode.duplicatedetector.i18n.Messages._;

import java.io.File;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import net.miginfocom.swing.MigLayout;

import com.googlecode.duplicatedetector.Constants;
import com.googlecode.duplicatedetector.controller.CloseActionListener;
import com.googlecode.duplicatedetector.controller.ProgressUpdater;
import com.googlecode.duplicatedetector.i18n.Keys;
import com.googlecode.duplicatedetector.model.DuplicateFinder;
import com.googlecode.duplicatedetector.model.Filter;

/**
 * Panel used when the duplicate detector is working. It shows the results.
 * 
 * @author Marco Biscaro
 */
public class WorkingPanel extends JPanel implements Constants, Keys {

	private static final long serialVersionUID = 1281047383763067135L;
	private JLabel progressLabel;
	private JProgressBar progressBar;
	private JLabel detailsLabel;
	private JTextArea detailsTextArea;
	private JButton closeButton;
	private transient DuplicateFinder finder;

	public WorkingPanel(Filter filter, List<File> files) {
		finder = new DuplicateFinder(filter, files.toArray(new File[files
				.size()]));
		initialize();
		new ProgressUpdater(finder, getProgressBar(), getDetailsTextArea());
		finder.execute();
	}

	private void initialize() {
		setLayout(new MigLayout(null, "[grow]", "[][][][grow][]"));
		add(getProgressLabel(), "wrap");
		add(getProgressBar(), "growx, wrap");
		add(getDetailsLabel(), "wrap");
		add(new JScrollPane(getDetailsTextArea()),
				"w :350:, h :250:, grow, wrap");
		add(getCloseButton(), "align right, w :100:");
	}

	private JLabel getProgressLabel() {
		if (progressLabel == null) {
			progressLabel = new JLabel();
			progressLabel.setText(_(PROGRESS));
		}
		return progressLabel;
	}

	private JProgressBar getProgressBar() {
		if (progressBar == null) {
			progressBar = new JProgressBar();
			progressBar.setStringPainted(true);
		}
		return progressBar;
	}

	private JLabel getDetailsLabel() {
		if (detailsLabel == null) {
			detailsLabel = new JLabel();
			detailsLabel.setText(_(DETAILS));
		}
		return detailsLabel;
	}

	private JTextArea getDetailsTextArea() {
		if (detailsTextArea == null) {
			detailsTextArea = new JTextArea();
			detailsTextArea.setEditable(false);
		}
		return detailsTextArea;
	}

	private JButton getCloseButton() {
		if (closeButton == null) {
			closeButton = new JButton();
			closeButton.setText(_(CLOSE));
			closeButton
					.addActionListener(new CloseActionListener(finder, this));
		}
		return closeButton;
	}

}
